
#ifndef __MY_ONLINE__
#define __MY_ONLINE__

#include"util.hpp"
#include<mutex>
#include<unordered_map>


class online_manager
{
   private:
   std::mutex _mutex;
   //使用哈希表来把websocket连接和id绑定在一起！！！！！
   std::unordered_map<uint64_t,wsserver_t::connection_ptr> _hall_user;
   std::unordered_map<uint64_t,wsserver_t::connection_ptr> _room_user;
   public:
   online_manager(){}

   //进入大厅或房间
   void enter_hall(uint64_t uid,wsserver_t::connection_ptr &conn)
   {
      std::unique_lock<std::mutex> lock(_mutex);
      _hall_user.insert(std::make_pair(uid,conn));
   }
   void enter_room(uint64_t uid,wsserver_t::connection_ptr &conn)
   {
      std::unique_lock<std::mutex> lock(_mutex);
      _room_user.insert(std::make_pair(uid,conn));
   }
   //退出大厅或房间
   void exit_hall(uint64_t uid)
   {
       std::unique_lock<std::mutex> lock(_mutex);
       _hall_user.erase(uid);
   }
   void exit_room(uint64_t uid)
   {
       std::unique_lock<std::mutex> lock(_mutex);
       _room_user.erase(uid);
   }
   
   //判断是否在大厅或房间
   bool is_in_hall(uint64_t uid)
   {
       std::unique_lock<std::mutex> lock(_mutex);
       auto pos = _hall_user.find(uid);
       if(pos != _hall_user.end())
       {
          return true;
       }
       return false;
   }
   bool is_in_room(uint64_t uid)
   {
       std::unique_lock<std::mutex> lock(_mutex);
       auto pos = _room_user.find(uid);
       if(pos != _room_user.end())
       {
          return true;
       }
       return false;
   }

   //通过用户id来获取其对应的连接，这样就可以给对方客户端发送信息了！！！！！！！！
   wsserver_t::connection_ptr get_conn_from_hall(uint64_t uid)
   {
       std::unique_lock<std::mutex> lock(_mutex);
       auto pos = _hall_user.find(uid);
       if(pos != _hall_user.end())
       {
          return pos->second;
       }
       return wsserver_t::connection_ptr();
   }

   wsserver_t::connection_ptr get_conn_from_room(uint64_t uid)
   {
       std::unique_lock<std::mutex> lock(_mutex);
       auto pos = _room_user.find(uid);
       if(pos != _room_user.end())
       {
          return pos->second;
       }
       return wsserver_t::connection_ptr();
   }
};
#endif